$(function(){
	$("#nav").accordion({animate: false});
	indexPage.setMenu();
	clockon();
})

var indexPage = {};
indexPage.setMenu = function(){
	var _menus = {};
    var userAuthID = $("#AuthID").val();
	if(userAuthID!=""){
		//同步获取
		$.ajax({
			type: 'POST',
			url: '/Main/GetMenu',
			data: userAuthID,
			async: false, //同步
			dataType: 'json',
			success: function (data) {
				$.each(data, function(i, menuitem){
					var menulist = "";
					menulist = "<ul>";
					$.each(menuitem.submenu, function(j,submenus){
						menulist += "<li><div><a ref='" + submenus.ID + "' href='#' rel='" + submenus.Url +"'><span class='icon " + submenus.WebIcon +"'>&nbsp;</span><span class='nav'>" + submenus.Name + "</span></a></div></li>";
						
					})
					menulist += "</ul>"
					$("#nav").accordion('add',{
						title: menuitem.menuname,
						content: menulist,
						iconCls: menuitem.icon,
						selected: false
					});
				})
			},
			error: function (xhr, status, error) {
				if(xhr.status == "400")
					indexPage.reLogin();
			}
		});
	}
	else
	{
		$.message.alert("错误","用户未登陆！","error")
	}
	
	$('.easyui-accordion li a').click(function () {
        var tabTitle = $(this).children('.nav').text();
        var url = $(this).attr("rel");
        var menuid = $(this).attr("ref");
        //var icon = getIcon(menuid, icon);
        indexPage.addTab(tabTitle, url);
        $('.easyui-accordion li div').removeClass("selected");
        $(this).parent().addClass("selected");
    }).hover(function () {
        $(this).parent().addClass("hover");
    }, function () {
        $(this).parent().removeClass("hover");
    });

    //选中第一个
    var panels = $("#nav").accordion('panels');
    var t = panels[0].panel('options').title;
    $('#nav').accordion('select', t);
}

indexPage.addTab = function(subtitle, url) {
    if (!$('#tabs').tabs('exists', subtitle)) {
        $('#tabs').tabs('add', {
            title: subtitle,
            //content: createFrame(url),
			href: url,
            closable: true,
            //icon: icon
			onLoadError: function(data, status, error){
				if(data.status == "400")
					indexPage.reLogin();
			}
        });
    } else {
        $('#tabs').tabs('select', subtitle);
        $('#mm-tabupdate').click();
    }
    //tabClose();
}

indexPage.resetPWD = function(){
	$("#resetPWD").dialog({
		title: "重设密码",
		modal: true,
		resizable: true,
		width: 300,
		height: 180,
		href: "system/resetpwd"
	});
}

indexPage.reLogin = function(){
	$("#reLogin").dialog({
		title: "重新登陆",
		modal: true,
		resizable: true,
		width: 300,
		height: 180,
		href: "Home/ReLogin"
	});
}

indexPage.Login = function(){
	if ($("input[name='login']").val() == "" || $("input[name='password']").val() == "") {
		$.messager.alert("错误","用户名或密码为空，请输入",'error');
		$("input[name='login']").focus();
	} else {
		    $("#reLoginForm").form("submit", {
				url: "Home/Login",
				onSubmit: function (param) {
					return $(this).form("validate");
				},
				success: function(data){
					var data = eval("(" + data + ")");
					if (data.success){
						$.messager.alert("提示",data.msg,"info");
						$("#reLoginForm").form("clear");
						$("#reLogin").dialog("close");
					}
					else{
						$.messager.alert("错误",data.msg,"error");
					}
				}
			});
	}
}

indexPage.savePWD = function(){
    $("#resetPWDForm").form("submit", {
        url: "system/SavePWD",
        onSubmit: function (param) {
            return $(this).form("validate");
        },
        success: function(data){
			var data = eval("(" + data + ")");
			if (data.success){
				$.messager.alert("提示",data.msg,"info");
				$("#resetPWDForm").form("clear");
				$("#resetPWD").dialog("close");
			}
			else{
				$.messager.alert("错误",data.msg,"error");
			}
		}
    });
}

indexPage.dlgClose = function(){
	$("#resetPWD").dialog("close");
}
function getIcon(menuid) {
    var icon = 'icon ';
    $.each(_menus.menus, function (i, n) {
        $.each(n.menus, function (j, o) {
            if (o.menuid == menuid) {
                icon += o.icon;
            }
        })
    })

    return icon;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children(".tabs-closable").text();
        $('#tabs').tabs('close', subtitle);
    })
    /*为选项卡绑定右键*/
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#mm').menu('show', {
            left: e.pageX,
            top: e.pageY
        });

        var subtitle = $(this).children(".tabs-closable").text();

        $('#mm').data("currtab", subtitle);
        $('#tabs').tabs('select', subtitle);
        return false;
    });
}

function createFrame(url) {
    var s = '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
    return s;
}

function clockon() {
    var now = new Date();
    var year = now.getFullYear(); //getFullYear getYear
    var month = now.getMonth();
    var date = now.getDate();
    var day = now.getDay();
    var hour = now.getHours();
    var minu = now.getMinutes();
    var sec = now.getSeconds();
    var week;
    month = month + 1;
    if (month < 10) month = "0" + month;
    if (date < 10) date = "0" + date;
    if (hour < 10) hour = "0" + hour;
    if (minu < 10) minu = "0" + minu;
    if (sec < 10) sec = "0" + sec;
    var arr_week = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    week = arr_week[day];
    var time = "";
    time = year + "年" + month + "月" + date + "日" + " " + hour + ":" + minu + ":" + sec + " " + week;

    $("#bgclock").html("<i class='fa fa-clock-o light-grey'></i>&nbsp;" + time);

    var timer = setTimeout("clockon()", 200);
}